//
//  QuDanMainTabBarController.m
//  QuDanOEM
//
//  Created by xiaohuang on 2021/10/18.
//

#import "QuDanMainTabBarController.h"
#import "QuDanHomeMainController.h"
#import "QuDanSquareMainController.h"
#import "QuDanMessageVC.h"
#import "QDMineVC.h"
#import "QuDanDynamicMainController.h"
#import "QuDanTabBar.h"
#import "PostNaviVC.h"
#import "QuDanCenterAlterView.h"
#import "QuDanSendSkilController.h"
#import "QuDanSendDongTaiController.h"
@interface QuDanMainTabBarController ()<QuDanTabBarDelegate,UITabBarControllerDelegate>
@end
@implementation QuDanMainTabBarController
+(void)initialize
{
    [super initialize];
    if (@available(iOS 15.0, *)) {
        [UITabBar appearance].tintColor = QD_MainThemColor;
        [UITabBar appearance].unselectedItemTintColor = QD_RGB(106, 107, 110);
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateSelected];
    } else {
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:QD_RGB(106, 107, 110)} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:QD_MainThemColor} forState:UIControlStateSelected];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    QuDanTabBar *tabbar = [[QuDanTabBar alloc] init];
    tabbar.myDelegate = self;
    [self setValue:tabbar forKeyPath:@"tabBar"];
    UIImageView*mView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cske_tabbar_bg"]];
    mView.contentMode = UIViewContentModeScaleAspectFill;
    mView.frame = CGRectMake(0,0,self.view.frame.size.width, 30);
    [tabbar insertSubview:mView atIndex:0];
    self.delegate = self;
    [self addChildViewControllers];
}
-(void)addChildViewControllers
{
    QuDanHomeMainController *homevc = [[QuDanHomeMainController alloc]init];
    [self addChildrenViewController:homevc
                           andTitle:@"首页"
                        andNorImage:@"tabbar_home_nor"
                        andSelImage:@"tabbar_home_sel" index:0];
    QuDanSquareMainController *squarevc = [[QuDanSquareMainController alloc]init];
    [self addChildrenViewController:squarevc
                           andTitle:@"广场"
                        andNorImage:@"tabbar_square_nor"
                        andSelImage:@"tabbar_square_sel" index:1];
    QuDanMessageVC *messagevc = [[QuDanMessageVC alloc]init];
    [self addChildrenViewController:messagevc
                           andTitle:@"消息"
                        andNorImage:@"tabbar_message_nor"
                        andSelImage:@"tabbar_message_sel" index:2];
    
    QDMineVC *aboutvc = [[QDMineVC alloc]init];
    [self addChildrenViewController:aboutvc
                           andTitle:@"我的"
                        andNorImage:@"tabbar_about_nor"
                        andSelImage:@"tabbar_about_sel" index:3];
    
}
- (void)addChildrenViewController:(BaseVC *)childVC andTitle:(NSString *)title andNorImage:(NSString *)norimage  andSelImage:(NSString *)SelImage index:(NSInteger)index{
    childVC.tabBarItem.image = [[UIImage imageNamed:norimage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 选中的颜色由tabbar的tintColor决定
    childVC.tabBarItem.selectedImage =  [[UIImage imageNamed:SelImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.title = title;
    childVC.index = index;
    PostNaviVC *nav = [[PostNaviVC alloc]initWithRootViewController:childVC];
    [self addChildViewController:nav];
}
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
#pragma mark -- UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//    [PostWindow toLogonAction:NO];
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}
#pragma mark --  QuDanTabBarDelegate
- (void)tabBarPlusSendBtnClick:(QuDanTabBar *)tabBar
{
    QuDanCenterAlterView *alterview = [QuDanCenterAlterView alertView];
    [alterview setQuDanCenterAlterViewDongtaiBlock:^{
        //动态
        NSLog(@"发布动态");
        QuDanSendDongTaiController *sendDongTaivc = [[QuDanSendDongTaiController alloc]init];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sendDongTaivc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.selectedViewController presentViewController:nav animated:YES completion:nil];
    }];
    [alterview setQuDanCenterAlterViewSkillBlock:^{
        //发布技能
        NSLog(@"发布技能");
        QuDanSendSkilController *sendSKillvc = [[QuDanSendSkilController alloc]init];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sendSKillvc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.selectedViewController presentViewController:nav animated:YES completion:nil];
    }];
    [alterview show];
}
@end
